<?php

namespace app\beautiful\middleware;

use app\beautiful\model\appointment\TechnicianModel;
use app\beautiful\model\StaffModel;
use app\common\model\ShopModel;


class StaffCheck{

    use \app\BaseTrait;



    /**

     * 处理请求

     *  验证是否登录是否正确的中间件

     * @param \think\Request $request

     * @param \Closure       $next

     * @return Response

     */

    public function handle($request, \Closure $next)

    {

        if(empty($request->member->mobile)){

            return $this->error('你并没有权限1',100);

        }

        $mobile=$request->member->mobile;

        if(null === $staff = StaffModel::where([

                ['mobile','=',$mobile],

                ['shop_id','=',$request->shopId],

                ['status','=',1],

                ['is_delete','=',0]

            ])->find()){

            return $this->error('你并没有权限2',100);

        }
        $shop=ShopModel::with(['detail'])->where([
            ['shop_id','=',$request->shopId]
        ])->find();
        $request->staff=$staff;
        $request->shopinfo=$shop;

        $request->staffId=$staff->staff_id;

        $technician = TechnicianModel::where([

            ['shop_id','=',$request->shopId],

            ['staff_id','=',$staff->staff_id],

            ['status','=',1],

            ['is_delete','=',0]

        ])->find();

        $request->technician_id=$technician?$technician->technician_id:0;

        return $next($request);

    }

}

